export default{
	data() {
		return {
			hasWeixinAuth: false,
			weixinAuthService:null
		}
	},
	computed:{
		userInfo(){
			return this.$store.state.chat.userInfo
		},
		appReady(){
			return this.$store.state.app.appReady
		}
	},
	watch:{
		'appReady':function(value){
			if(value && this.userInfo.uid){
				uni.reLaunch({
				    url: '/pages/my/index'
				});
			}
		}
	},
	onLoad:function(e){
		if(this.appReady && this.userInfo.uid){
			uni.reLaunch({
			    url: '/pages/my/index'
			});
		}
		// #ifdef APP-PLUS
		plus.oauth.getServices((services) => {
			this.weixinAuthService = services.find((service) => {
				return service.id === 'weixin'
			})
			if (this.weixinAuthService) {
				this.hasWeixinAuth = true
			}
		});
		// #endif
	},
	// #ifdef APP-PLUS
	onBackPress:function(event) {
		if (event.from === 'navigateBack') {  
			return false;  
		}
		if(getCurrentPages().length<2){
			 var main = plus.android.runtimeMainActivity();  
			    main.moveTaskToBack(false);
			//plus.runtime.quit();
		}
	},
	// #endif
	methods:{
		getWeixinCode() {
			return new Promise((resolve, reject) => {
				// #ifdef APP-PLUS
				this.weixinAuthService.authorize(function(res) {
					resolve(res.code)
				}, function(err) {
					console.log(err)
					reject(new Error('微信登录失败'))
				});
				// #endif
				// #ifdef MP-WEIXIN
				uni.login({
					provider: 'weixin',
					success(res) {
						resolve(res.code)
					},
					fail(err) {
						reject(new Error('微信登录失败'))
					}
				})
				// #endif
			})
		},
		code2SessionWeixin() {
			
		},
		unbindWeixin() {
		
		},
		bindWeixin() {
			
		},
		loginByWeixin() {
			let self=this
			this.getWeixinCode().then((code) => {
				// #ifdef APP-PLUS
				const platform='app-plus'
				// #endif
				// #ifdef MP-WEIXIN
				const platform='mp-weixin'
				// #endif
				return  this.$request('login/weixing', {code,platform})
				
			})
			.then((res) => {
				const {openid}=res
				uni.setStorage({
					key:'openid',
					data:openid
				})
				// #ifdef APP-PLUS
				return this.getWeixinCode().then((code)=>{
					if (res.code === 0) {
						return this.$request('login/getUserInfo', {code})
						.then(userInfo=>{
							const {nickname,headimgurl}=userInfo
							res={...res,username:nickname,nickname,avatar:headimgurl}
							this.setStorageSync(res)
						})
					}
					return Promise.reject({message:'登录失败'})
				})
				// #endif
				// #ifdef MP-WEIXIN
				const userInfo=uni.getStorageSync('mp_wexing_user_info')
				if (res.code === 0) {
					const {nickName,avatarUrl}=userInfo
					res={...res,username:nickName,nickname:nickName,avatar:avatarUrl}
					this.setStorageSync(res)
				}else{
					return Promise.reject({message:'登录失败'})
				}
				// #endif
			}).catch((e) => {
				uni.showModal({
					showCancel: false,
					content: '微信登录失败，请稍后再试'
				})
			})
		},
		getuserinfo(e){
			if(e.detail.errMsg==='getUserInfo:ok'){
				const {userInfo}=e.detail
				uni.setStorage({
					key:'mp_wexing_user_info',
					data:userInfo
				}).then(()=>{
					this.loginByWeixin()
				})
			}else{
				uni.showModal({
					title:"请授权登录"
				})
			}
		},
		setStorageSync(res) {
			let {
				username,
				uid,
				nickname,
				avatar
			} = res
			const UserInfo = {
				username,
				uid,
				nickname,
				avatar,
				figureurl: avatar,
				id: uid,
			}
			this.$store.commit('chat/setUserInfo', UserInfo)
			uni.setStorageSync('user_info', UserInfo)
			uni.reLaunch({
				url: '/pages/my/index'
			});
		}
	},
	
}